package com.yxb.api.rest;


import com.yxb.config.rest.RestResult;
import com.yxb.service.common.domain.UserDevice;
import com.yxb.service.common.interf.UserDeviceService;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/userDevice")
public class UserDeviceController {

    @Resource
    private UserDeviceService  userDeviceService;

    @GetMapping("/user/{userId}/list")
    public ResponseEntity<RestResult> userList(@PathVariable String userId){
        List<UserDevice>  userDeviceList = userDeviceService.queryListByUserId(userId, null);
        return ResponseEntity.ok(RestResult.success(userDeviceList));
    }

    @GetMapping("/device/{deviceId}/list")
    public ResponseEntity<RestResult> deviceList(@PathVariable String deviceId){
        List<UserDevice>  userDeviceList = userDeviceService.queryListByDeviceId(deviceId, null);
        return ResponseEntity.ok(RestResult.success(userDeviceList));
    }

    @RequestMapping("/bind")
    public ResponseEntity<RestResult> saveUserDevice(String userId, String deviceId){
        UserDevice  userDevice  = new UserDevice();
        userDevice.setDeviceId(deviceId);
        userDevice.setUserId(userId);
        userDevice.setUpdatedBy(userId);
        ImmutableTriple<Boolean, String , UserDevice> userDeviceImmutableTriple = userDeviceService.addUserDevice(userDevice);
        if (!userDeviceImmutableTriple.getLeft() ){
            return ResponseEntity.ok(RestResult.commonError(userDeviceImmutableTriple.getMiddle()));
        }
        return ResponseEntity.ok(RestResult.success());
    }

    @RequestMapping("/unbind")
    public ResponseEntity<RestResult> unBindUserDevice(String userId, Integer id){
        ImmutablePair<Boolean, String> unbindImmutablePair = userDeviceService.unbind(id, userId);
        if (!unbindImmutablePair.getLeft() ){
            return ResponseEntity.ok(RestResult.commonError(unbindImmutablePair.getRight()));
        }
        return ResponseEntity.ok(RestResult.success());
    }



}
